var config = require('../../../../utils/config.js');
var config = require('../../../../utils/config.js');
var util = require('../../../../utils/util');
var API_URL = config.API_URL;
var userInfo;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    oldPW:'',
    newPW:'',
    rePW:'',
    oldF: true,
    newF: false,
    reF: false,
    old_text: [' ', ' ', ' ', ' ', ' ', ' '],
    new_text: [' ', ' ', ' ', ' ', ' ', ' '],
    re_text: [' ', ' ', ' ', ' ', ' ', ' '],
    disabled:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var tag = options.tag;
    userInfo = my.getStorageSync({key:'user'}).data
    this.setData({
      tag:tag
    })
    if(tag!='edit'){
      this.setData({
        oldPW : [' ', ' ', ' ', ' ', ' ', ' ']
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  /* onShareAppMessage: function () {
  
  } */
  keyboard: function (e) {
    var tag = e.currentTarget.dataset.tag;
    switch(tag){
      case 'old': this.setData({ oldF: true }); break;
      case 'new': this.setData({ newF: true }); break;
      case 're': this.setData({ reF: true }); break;
    }
  },
  // 输入密码验证
  password: function (e) {
    var that = this;
    var tag = e.currentTarget.dataset.tag;
    var password = e.detail.value;
    var length = password.length;
    var password_text,disabled;
    switch (length) {
      case 0: password_text = [' ', ' ', ' ', ' ', ' ', ' ']; break;
      case 1: password_text = ['●', ' ', ' ', ' ', ' ', ' ']; break;
      case 2: password_text = ['●', '●', ' ', ' ', ' ', ' ']; break;
      case 3: password_text = ['●', '●', '●', ' ', ' ', ' ']; break;
      case 4: password_text = ['●', '●', '●', '●', ' ', ' ']; break;
      case 5: password_text = ['●', '●', '●', '●', '●', ' ']; break;
      case 6: password_text = ['●', '●', '●', '●', '●', '●']; break;
    }
    switch (tag) {
      case 'old': var newPW = that.data.newPW; var rePW = that.data.rePW; if (password.length==6&&newPW.length==6 && rePW.length==6){disabled=false;} else{disabled=true;} that.setData({oldPW: password,old_text: password_text,disabled:disabled }); break;
      case 'new': var oldPW = that.data.oldPW; var rePW = that.data.rePW; if (oldPW.length == 6 && password.length == 6 && rePW.length == 6) { disabled = false; } else { disabled = true; } that.setData({ newPW: password, new_text: password_text, disabled: disabled }); break;
      case 're': var oldPW = that.data.oldPW; var newPW = that.data.newPW; if (oldPW.length == 6 && newPW.length == 6 && password.length == 6) { disabled = false; } else { disabled = true; } that.setData({ rePW: password, re_text: password_text, disabled: disabled }); break;
    }
  },
  submit:function(e){
    var that = this;
    var oldPW = that.data.oldPW;
    var newPW = that.data.newPW;
    var rePW = that.data.rePW;
    var tag = that.data.tag;
    if(rePW != newPW){
      util.showNotice('两次输入的新密码不一样')
      return
    }
    if(tag=='edit'){
      my.httpRequest({
        url: API_URL.edit_payPW,
        data: {
          user_id: userInfo.userId,
          old_password: oldPW,
          new_password: newPW
        },
        method: 'POST',
        success: function(res) {
          if(res.data.status==200){
            my.navigateBack({
              delta: 2,
            })
          }else{
            util.showNotice('网络错误')
          }
        },
        fail: function(res) {
          util.showNotice('请求超时')
        },
        complete: function(res) {},
      })
    }else{
      my.httpRequest({
        url: API_URL.set_payPW,
        data: {
          user_id: userInfo.userId,
          password: newPW
        },
        method: 'POST',
        success: function (res) {
          console.log(res);
          if (res.data.status == 200) {
            my.navigateBack({
              delta: 2,
            })
          } else {
            util.showNotice('网络错误')
          }
        },
        fail: function (res) {
          util.showNotice('请求超时')
        },
        complete: function (res) { },
      })
    }
  }
})